<template>
  <div>
    <SearchContainer @search="getData" @reset="() => reset()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="ml-2">
          <a-input v-model:value="formState.zjm" class="w-72" placeholder="住院号/姓名/助记码" allow-clear @keydown.enter="getData" />
        </a-form-item>
        <a-form-item name="dateRange" label="入院日期">
          <a-range-picker v-model:value="formState.dateRange" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="onChangeRangePicker" />
        </a-form-item>
        <a-form-item name="dateRange" label="挂账日期">
          <a-range-picker v-model:value="formState.dateRange1" class="w-64" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="onChangeRangePicker" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2">
      <BaseTable fill :data-source="tableData" :loading="loading" :scroll="{ x: 2100 }" column-code="000915" :pagination="pagination">
        <template #bodyCell="{ column, index, record }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'chargeStatus'">
            <a-tag v-if="record.chargeStatus === 0" color="blue">未结账</a-tag>
            <a-tag v-else-if="record.chargeStatus === 1" color="green">已结账</a-tag>
            <a-tag v-else-if="record.chargeStatus === 2" color="red">退费中</a-tag>
            <a-tag v-else-if="record.chargeStatus === 3" color="orange">已挂账</a-tag>
            <span v-else>——</span>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onOpenHospitalSettlement(record)">住院结算</a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import dayjs from 'dayjs'
import { useRouter } from 'vue-router'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const router = useRouter()
const formState = ref({
  stdate: dayjs(new Date().setDate(new Date().getDate() - 7)).format('YYYY-MM-DD'),
  eddate: dayjs().format('YYYY-MM-DD'),
  zjm: '',
  dateRange: [dayjs(new Date().setDate(new Date().getDate() - 7)).format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  dateRange1: []
})
const { loading, tableData, pagination, getData } = useTable({
  url: '/inp/zydj/getInpRecordList',
  immediate: true,
  params: formState.value,
  formatParams: () => {
    return {
      stdate: formState.value.dateRange?.[0],
      eddate: formState.value.dateRange?.[1],
      creditTimeStart: formState.value.dateRange1?.[0],
      creditTimeEnd: formState.value.dateRange1?.[1],
      zjm: formState.value.zjm,
      chargeStatus: '3'
    }
  }
})

const onOpenHospitalSettlement = async () => {
  router.push({
    path: '/in-registration/hospital-settlement'
  })
}
const onChangeRangePicker = (e) => {
  formState.value.stdate = e[0]
  formState.value.eddate = e[1]
}
const reset = () => {
  formRef.value.resetFields()
  getData()
}
</script>
